package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityDishesClass;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDishesClass;
import com.ocom.payconfig.service.DishesClassService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishesClassServiceImpl extends ServiceImpl<IDaoDishesClass, EntityDishesClass> implements DishesClassService {


    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesClass> dishesClassList(Long comId, Integer merchId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq(ObjectUtil.isNotEmpty(merchId) && merchId > 0,"merch_id",merchId);
        queryWrapper.orderByAsc("parent_id","sort");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesClass> dishesClassOfCom(Long comId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.orderByAsc("merch_id","parent_id","sort");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectIsParentId(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        queryWrapper.eq("dishes_class_id",jsonObject.getInteger("parentId"));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectIsMerchId(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        queryWrapper.eq("dishes_class_name",jsonObject.getString("className"));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean newDishesClass(Long comId, EntityDishesClass newDishesClass) {
        return this.save(newDishesClass);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectClassName(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        queryWrapper.eq("dishes_class_name",jsonObject.getString("className"));
        queryWrapper.ne("dishes_class_id",jsonObject.getInteger("dishesClassId"));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean updateDishesClass(Long comId, EntityDishesClass newDishesClass) {
        return this.updateById(newDishesClass);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectParentId(Long comId, Integer merchId, Integer dishesClassId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.eq("parent_id",dishesClassId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectDishesClassId(Long comId, Integer merchId, Integer dishesClassId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.eq("dishes_class_id",dishesClassId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public boolean removeDishesClass(Long comId, Integer merchId, Integer dishesClassId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.eq("dishes_class_id",dishesClassId);
        return this.remove(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectDishesClassIds(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("GROUP_CONCAT(dishes_class_id) as classIds");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectDishesClassIdEs(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("GROUP_CONCAT(dishes_class_id) as classIds");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",jsonObject.getInteger("merchId"));
        /*String[] arr=jsonObject.getString("parentIds").split(",");
        queryWrapper.in("parent_id",arr);*/
        queryWrapper.eq("parent_id",jsonObject.getString("parentIds"));
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesClass selectrMerchs(Long comId, Integer merchId) {
        QueryWrapper<EntityDishesClass> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("GROUP_CONCAT(merch_id) as strMerchs");
        queryWrapper.eq("com_id",comId);
        queryWrapper.ne("merch_id",0);
        return this.getOne(queryWrapper);
    }
}
